const storageManager = require('../storage');

/**
 * 项目管理工具
 */
module.exports = {
  name: 'project',
  description: '项目管理工具，用于创建、切换和列出项目',
  parameters: {
    type: 'object',
    properties: {
      action: {
        type: 'string',
        enum: ['list', 'create', 'switch', 'current'],
        description: '要执行的操作: list(列出所有项目), create(创建新项目), switch(切换项目), current(获取当前项目)'
      },
      name: {
        type: 'string',
        description: '项目名称，用于创建或切换项目'
      },
      description: {
        type: 'string',
        description: '项目描述，用于创建新项目'
      }
    },
    required: ['action']
  },
  handler: async ({ action, name, description }) => {
    try {
      switch (action) {
        case 'list':
          const projects = storageManager.getProjectList();
          return {
            projects,
            currentProject: storageManager.getCurrentProject()
          };
        
        case 'create':
          if (!name) {
            throw new Error('创建项目需要提供项目名称');
          }
          const newProject = storageManager.createProject(name, description || '');
          return {
            message: `项目 ${name} 创建成功`,
            project: newProject.project
          };
        
        case 'switch':
          if (!name) {
            throw new Error('切换项目需要提供项目名称');
          }
          const project = storageManager.loadProject(name);
          return {
            message: `已切换到项目 ${name}`,
            project: project.project
          };
        
        case 'current':
          const currentProject = storageManager.getCurrentProject();
          const projectData = storageManager.loadProject(currentProject);
          return {
            currentProject,
            project: projectData.project
          };
        
        default:
          throw new Error(`未知操作: ${action}`);
      }
    } catch (error) {
      return {
        error: error.message
      };
    }
  }
}; 